
var callData = {}
import $ from '@/common'
import TKY from './tky/tky';
import config from './tky/config';
const Phone = {
  info: {
    uname: '1046@zxyl.com',
  },
  path_data: 'service/{app}/collector/phonebar',
  channel: {
    qm: '010',
    tky: '020'
  },
  // 拨打调用
  isLoad: false,
  // 加载状态，防止重复加载
  loading: false,
  // 等待呼叫的号码，当呼出时若软电话加载完成，则记录号码，直到加载完成后进行呼出
  waitNumber: null,

  init() {
    if (this.isLoad || this.loading) return new Promise(resolve => resolve())
    return this.login()
  },
  destroy() {
    this.isLoad = false
    window.phone.destroy(true)
  },
  complete(success, error) {
    Phone.isLoad = true
    if (Phone.waitNumber) {
      $.webcall.callNumber(Phone.waitNumber, success, error)
    // Phone.call(Phone.waitNumber)
    }
  },
  call(number, success, error) {
    if (!Phone.isLoad) {
      // 未初始化则尝试初始化并记录号码
      Phone.waitNumber = number
      // 调用电话组件
      $.webcall.initPhone()
    // this.init()
    } else {
      if (!this.getIsTky()) {
        window.softphoneBar.dialout(number)
        return;
      }
      this.tky.callout(number)
    }
  },
  // 远程查询
  getCallData() {},
  // 软电话登录验证
  login() {
    return new Promise((resolve, reject) => {
      Phone.requirePhoneSript().then(() => {
        if (Phone.info.cti_channel_id === Phone.channel.qm) { // 容联七陌登录
          let hojo = window.hojo
          hojo.registerModulePath('icallcenter', '../js/icallcenter')
          hojo.require('icallcenter.logon')
          hojo.require('hojo.io.script')
          var get = hojo.io.script.get
          hojo.io.script.get = (c) => {
            var d = get.call(hojo.io.script, c)
            // 可在此处对插件内请求错误进行处理
            return d
          }

          window.icallcenter.onError = (res) => {
            window.LinkErrorHandler('软电话' + res)
            if ($.webcall) {
              $.webcall.loginToError(res)
            }
          }
          window.icallcenter.onRing = (res) => {
            console.info(res)
          }
          hojo.addOnLoad(function () {
            window.icallcenter.logon.startLogon(Phone.info.seats_code, Phone.info.seats_pwd, Phone.info.proxy, Phone.info.extenType, Phone.info.monitor)
            resolve()
          })
          hojo.addOnWindowUnload(function () {
            if (window.phone) {
              window.phone.destroy(true)
            }
          })
          return;
        }

        // 天客云登录
        let tky = this.tky || new TKY({
          cno: Phone.info.seats_code, // 坐席号
          pwd: Phone.info.seats_pwd, // 坐席号密码
          bindTel: Phone.info.primary_number, // 绑定硬件话机号码
          bindType: '1', // 绑定类型
          initStatus: 'online', // 初始化状态
          baseUrl: Phone.info.proxy,
          hotLine: ''
        });
        this.tky = tky;
        tky.login();
      })
    })
  },
  requirePhoneSript() {
    return new Promise((resolve) => {
      this.getSeatNumber().then((data) => {
        if (data.cti_channel_id === this.channel.qm) { // 容联七陌
          Promise.all([
            $.requireScript('/static/edb_bar/js/icallcenter/global.js'),
            $.requireScript({
              src: '/static/edb_bar/hojo/hojo.js',
              djConfig: 'isDebug:false, parseOnLoad:false'
            }),
          ]).then(resolve)
          return;
        }

        // 天客云
        $.requireScript('/static/tky/index.js').then(resolve)
      })
    })
  },
  getSeatNumber() {
    return new Promise((resolve, reject) => {
      $.link(Phone.path_data).then((res) => {
        if (res.data) {
          if (res.data.is_mobile) {
            res.data.monitor = true
            res.data.extenType = 'Local'
          } else {
            res.data.monitor = false
            res.data.extenType = 'gateway'
          }
          Phone.info = $.setObj(res.data, Phone.info)
          resolve(res.data);
        }
      })
    })
  },
  getIsTky: () => Phone.info.cti_channel_id === Phone.channel.tky,

  hangup: () => {
    Phone.tky.hangup()
  }, // tky挂断电话
  statusList: {
    stInnerDialing: {
      text: '呼叫中',
      class: 'blue'
    },
    stInnerTalking: {
      text: '内部通话',
      class: 'azure'
    },
    stInvalid: {
      text: '空闲',
      class: 'green',
      icon: 'coffee'
    },
    stRest: {
      text: '小休',
      class: 'orange',
      icon: 'chatbox-working'
    },
    stBusy: {
      text: '忙碌',
      class: 'red',
      icon: 'clock'
    },
    stAbate: {
      text: '失效',
      class: 'gray'
    },
    // 外部拨打，当前用户未接状态
    stBelling: {
      text: '来电振铃',
      class: 'blue'
    },
    // 外部拨打，当前用户接听中
    stTalking: {
      text: '普通通话',
      class: 'azure'
    },
    stListening: {
      text: '监听振铃',
      class: 'blue'
    },
    stListened: {
      text: '监听中',
      class: 'blue'
    },
    // ??呼出时触发，可通过其下阶段的状态判断对方界定状态
    stDialing: {
      text: '呼叫中',
      class: 'blue'
    },
    // 呼出号码，对方接听,可用于判断对方是否接听
    stDialTalking: {
      text: '外呼通话',
      class: 'azure'
    },
    stHold: {
      text: '保持',
      class: 'blue'
    },
    stInnerBelling: {
      text: '来电振铃',
      class: 'blue'
    },
    stThreeWayTalking: {
      text: '三方通话',
      class: 'azure'
    },
    ...config
  },
callData}

export default Phone
